package model;



import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import utilities.Utilities;

public class GameChain extends AbstractChain {	
	
	public GameChain(){
		super();
	}
	
	/**
	 * Remove the player in the specified seat from the game.
	 * If that player had the button, hand it to the person before him. 
	 * This needs to be tested, it might go to the next player
	 * @param seat
	 */
	public void remove(int seat){		
		Player playertoRemove = getPlayer(seat);
		boolean hasButton = playertoRemove.hasButton();
		super.remove(seat);
		
		if(hasButton){
			getPlayer(activePlayerSeat).giveButton();
		}
	}
	
	
	/**
	 * Get a reference to the next player. This also passes the dealer button along
	 * @return
	 */
	public Player getNextPlayer(){
		// Take the button from the dealer
		Player p = map.get(activePlayerSeat);		
		p.takeButton();
		
		// Iterate players and hand him the button
		p = super.getNextPlayer();
		p.giveButton();	
		
		return p;
	}
	
	
	/**
	 * Get a reference to the previous player. This also passes the dealer button back
	 * @return
	 */
	public Player getPrevPlayer(){		
		// Take the button from the dealer
		Player p = map.get(activePlayerSeat);		
		p.takeButton();
		
		// Iterate players and hand him the button
		p = super.getPrevPlayer();
		p.giveButton();	
		
		return p;
	}
	
	/**
	 * Create a hand chain instance.
	 * @return
	 */
	public HandChain getHandChain(){
		HandChain other = new HandChain();
		
		Set<Integer> keys = map.keySet();
		for(Integer i : keys){
			other.addPlayer(i, map.get(i));			
		}
		
		other.initializeSeats(getDealerPlayer().getSeat());
		
		return other;
	}
	

}
